package com.erp.app.report.genericInquiry.inquiryCriteria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.report.genericInquiry.inquiryCriteria.beans.vo.InquiryCriteriaVO;
import com.erp.app.report.genericInquiry.inquiryMaster.ejb.GenericInquiryMasterPK;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.dao.SimplePageEditStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;

public class InquiryCriteriaDAO extends SimplePageEditStyleDao
{
//  to be implemented when needed
    protected String sqlPurge(){return null;}

//  to be implemented when needed
    protected void setValuesForPurge(PreparedStatement pStmt, Object obj) throws SQLException{}

//  to be implemented when needed
    protected String sqlUpdate(){return null;}

//  to be implemented when needed
    protected void setValuesForModify(PreparedStatement pStmt, Object obj) throws SQLException{}
    
//  to be implemented when needed
    protected String sqlFind(){return null;}

//  to be implemented when needed
    protected void setValuesForFind(PreparedStatement pStmt, Object obj) throws SQLException{}

//  to be implemented when needed
    protected String sqlInsert(){return null;}

//  to be implemented when needed
    protected void setValuesForInsert(PreparedStatement pStmt, Object obj) throws SQLException{}

//  to be implemented when needed
    protected String sqlDelete(){return null;}

//  to be implemented when needed
    protected void setValuesForDelete(PreparedStatement pStmt, Object obj) throws SQLException{}


    protected String sqlList(){return sqlList(null, null);}
        
    public ChildList selectByParent(SimpleBaseVo parent, Connection conn) throws FindRecordException {
		return super.selectByParent(parent, InquiryCriteriaVO.class, conn);
	}

    protected String toWhereClause(Object obj) 
	{
		return "";
	}
	
	protected void setValuesForCriteria(PreparedStatement pStmt, Object obj)
	throws SQLException { 
	}

    protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException{
        InquiryCriteriaVO hdrVo = new InquiryCriteriaVO();
        hdrVo.setInquiryType(rs.getString("inquiry_type"));
        hdrVo.setLineNo(rs.getBigDecimal("line_no"));
        hdrVo.setFieldCode(rs.getString("field_code"));
        hdrVo.setFieldName(rs.getString("field_name"));
        hdrVo.setFieldType(rs.getString("field_type"));
        hdrVo.setFieldNature(rs.getString("field_nature"));
        hdrVo.setDdSelect(rs.getString("dd_select"));
        hdrVo.setDdValue(rs.getString("dd_value"));
        hdrVo.setDdFrom(rs.getString("dd_from"));
        hdrVo.setDdWhere(rs.getString("dd_where"));
        hdrVo.setDdOrder(rs.getString("dd_order"));
        hdrVo.setDdShare(rs.getString("dd_share"));
        hdrVo.setMddHeight(rs.getString("mdd_height"));
        hdrVo.setMaxLength(rs.getBigDecimal("max_length"));
        hdrVo.setFieldSize(rs.getBigDecimal("field_size"));
        hdrVo.setMandatoryFlag(rs.getString("mandatory_flag"));
        hdrVo.setDefaultValueFm(rs.getString("default_value_fm"));
        hdrVo.setDefaultValueTo(rs.getString("default_value_to"));        
        return hdrVo;
    }
    
	protected String sqlList(Object parent, Object child) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("SELECT a.inquiry_type inquiry_type, a.line_no line_no, a.field_code field_code, nvl(b.label, a.field_name) field_name, a.field_type field_type");
		sb.append(" , a.field_nature field_nature, a.dd_select dd_select, a.dd_value dd_value, a.dd_from dd_from, a.dd_where dd_where");
		sb.append(" , a.dd_order dd_order, a.dd_share dd_share, a.mdd_height mdd_height, a.max_length max_length, a.field_size field_size");
		sb.append(" , a.mandatory_flag mandatory_flag, a.default_value_fm default_value_fm, a.default_value_to default_value_to, a.create_user create_user, a.create_date create_date");
		sb.append(" , a.update_user update_user, a.update_date update_date");
		sb.append(" FROM t_inquiry_criteria a, t_inquiry_criteria_label b");
		sb.append(sqlListWhereClause(child));
		sb.append(" ORDER BY a.line_no asc"); 
        super.logSqlQuery(sb.toString());
		
		return sb.toString();
	}

	protected String sqlListWhereClause(Object bizModel) {
		StringBuffer sb = new StringBuffer();
		
		sb.append(" WHERE a.inquiry_type = ? AND a.inquiry_type = b.inquiry_type(+) AND a.line_no = b.line_no(+)");
		sb.append(" AND b.lang(+) = ?");
			
		return sb.toString();
	}

	protected void setValuesForList(PreparedStatement pStmt, Object bizModel)
			throws SQLException {
	    int i = 1;
        GenericInquiryMasterPK pk = (GenericInquiryMasterPK) bizModel;
	    pStmt.setString(i++, pk.getInquiryType());
	    pStmt.setString(i++, pk.getLang());
        
        //System.out.println("****************************, setValuesForList(), inqType=" + pk.getInquiryType());
        //System.out.println("****************************, setValuesForList(), lang=" + pk.getLang());
	}

	protected String findByCriteriaOrderBy(SimpleBaseVo model, Object obj)
	{
		return " INQUIRY_TYPE, LINE_NO";
	}

}
